package com.iwhalecloud.bss.kite.cucc.dataservice.config;

import com.iwhalecloud.bss.kite.cart.common.datasource.IDataSourceConfig;
import com.iwhalecloud.bss.kite.common.datasource.DataSourceName;
import com.iwhalecloud.bss.kite.datasource.router.DataSourceRouter;
import com.ztesoft.bss.common.util.PropertyConfigurer;
import com.ztesoft.zsmart.core.jdbc.CoreJdbcTemplate;
import com.ztesoft.zsmart.core.jdbc.datasource.SqlSettings;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源配置
 */
@Configuration
@EnableTransactionManagement
@ConditionalOnMissingBean(IDataSourceConfig.class)
@ImportResource("classpath:spring/datasource.xml")
public class DataSourceConfig {

    /**
     * 受理中心数据源
     */
    @Resource
    private DataSource dataSourceApp;

    /**
     * 全局序列数据源
     */
    @Resource
    private DataSource dataSource4GlobalSeq;

    /**
     * 配置库数据源
     */
    @Resource
    private DataSource dataSourceAppConfig;

    /**
     * 多数据源配置
     *
     * @return
     */
    @Primary
    @Bean("dataSource")
    public DataSource dataSource() {
        DataSourceRouter dataSource = new DataSourceRouter();

        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceName.dataSourceApp, dataSourceApp);
        targetDataSources.put(DataSourceName.dataSource4GlobalSeq, dataSource4GlobalSeq);
        targetDataSources.put(DataSourceName.dataSourceAppConfig, dataSourceAppConfig);

        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(dataSourceApp);

        return dataSource;
    }

    /**
     * 受理中心JdbcTemplate
     *
     * @param dataSource
     * @return
     */
    @Primary
    @Bean("jdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
        return new CoreJdbcTemplate(dataSource);
    }

    /**
     * 全局序列JdbcTemplate
     *
     * @param dataSource
     * @return
     */
    @Bean("jdbcTemplate4GlobalSeq")
    public JdbcTemplate jdbcTemplate4GlobalSeq(@Qualifier("dataSource4GlobalSeq") DataSource dataSource) {
        return new CoreJdbcTemplate(dataSource);
    }

    /**
     * 配置库JdbcTemplate
     *
     * @param dataSource
     * @return
     */
    @Bean("jdbcTemplateConfig")
    public JdbcTemplate jdbcTemplateConfig(@Qualifier("dataSourceAppConfig") DataSource dataSource) {
        return new CoreJdbcTemplate(dataSource);
    }

    /**
     * 定时任务JdbcTemplate，指定使用配置库数据源
     * @param dataSource
     * @return
     */
    @Bean("jdbcTemplate4Job")
    public JdbcTemplate jdbcTemplate4Job(@Qualifier("dataSourceAppConfig") DataSource dataSource) {
        return new CoreJdbcTemplate(dataSource);
    }

    /**
     * 初始化SqlSettings才能初始化数据源{@link com.ztesoft.zsmart.core.jdbc.datasource.CoreDataSourceBean}}
     *
     * @param propertyConfigurer
     * @return
     */
    @Bean
    @Primary
    public SqlSettings sqlSettings(PropertyConfigurer propertyConfigurer) {
        SqlSettings sqlSettings = new SqlSettings();
        String limitResultSet = propertyConfigurer.getProperty("ftf.sql.limit-result-set");
        String limitConcurrent = propertyConfigurer.getProperty("ftf.sql.limit-concurrent");
        sqlSettings.setLimitResultSet(NumberUtils.toInt(limitResultSet, -1));
        sqlSettings.setLimitConcurrent(NumberUtils.toInt(limitConcurrent, -1));

        return sqlSettings;
    }
}
